package com.nuotu.datawall.clouddata.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nuotu.datawall.clouddata.MainActivity;
import com.nuotu.datawall.clouddata.R;
import com.nuotu.datawall.clouddata.util.ToastUtils;
import com.nuotu.datawall.clouddata.util.Tool;


public class ConfirmAlert {
    private static ConfirmAlert confirmAlert ;

    //使用饿汉单例模式
    public static ConfirmAlert getInstance() {
        if(confirmAlert==null){
            confirmAlert= new ConfirmAlert();
        }
        return confirmAlert;
    }
    private MyDialog setDatedialog;
    private Context mContext;
    private ToDo tddd;
    public static final String CONFIG_PASSWORD = "nuotu.net";
    private static boolean isFlag=true;
    public ConfirmAlert init(final Activity context) {
        if(isFlag){
            isFlag=false;
            return this;
        }
        mContext = context;
        View view  = context.getLayoutInflater().inflate(R.layout.dialog_setting_edit_b, null);
        final TextView title = (TextView) view.findViewById(R.id.tv_edit_title);
        final EditText et_data = (EditText) view.findViewById(R.id.tv_edit_content);
        et_data.setInputType(129);
//        et_data.setText("nuotu.net");
        title.setText("登陆");
        Button btnOk = (Button) view.findViewById(R.id.btn_ok);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
//      et_data.setText("nuotu");
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 判断二次输入的密码是否一至，如果一样就保存起来
                String str = et_data.getText().toString();
                if (TextUtils.isEmpty(str)) {
                    ToastUtils.toastShort("请输入密码");
                    return;
                }
                if (CONFIG_PASSWORD.equals(str)) {
                    Tool.overInputkeyboard(mContext, et_data);
                    setDatedialog.dismiss();
                    tddd.todo();
                } else {
                    ToastUtils.toastLong("密码错误");
                }
            }
        });
        // 取消按钮的点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭键盘
                isFlag=true;
                Tool.overInputkeyboard(mContext, et_data);
                setDatedialog.dismiss();
            }
        });

        setDatedialog = new MyDialog(context);
        setDatedialog.setOnBeforeDismissListener(new MyDialog.OnBeforeDismissListener() {
            @Override
            public void onDismiss() {
                Tool.overInputkeyboard(mContext, et_data);

            }
        });
        setDatedialog.setCancelable(true); //弹出后会点击屏幕或物理返回键，false:dialog不消失  true:消失
        setDatedialog.setView(view, 0, 0, 0, 0); // 设置view 和左上右下的间距
        if(!setDatedialog.isShowing()){
            setDatedialog.show();
        }
        setDatedialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
        setDatedialog.getWindow().getDecorView().setSystemUiVisibility(View.GONE);
        setDatedialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置窗口的大小
        setDatedialog.getWindow().setLayout(800, 400);
        return this;
    }
    public ConfirmAlert setOk(ToDo rr) {
        this.tddd = rr;
        return this;
    }

    public interface ToDo {
        void todo();
    }

}
